﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using CrystalDecisions.Shared;

namespace _201206QuanLyBanHang.BaoCao
{
    public partial class FormDanhSachKhachHang : Form
    {
        //define  
        private Boolean USE_CHECK_AlREADY_CB = true;
        private Boolean USE_CHECK_AlREADY_TABLE = true;
        private SQLCommand     msql = new SQLCommand();
        private DataConn       dtconn = new DataConn();
        DataSet ds_kh = new DataSet();
      
        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        DataTable dttable = new DataTable();      
        
        //variable
       public int makv;
       public string tenkv,nguoinhan,nguoixuat;
       FormHienThiKhachHang fromkh;
               


        public FormDanhSachKhachHang()
        {
            InitializeComponent();
            #region
                // load combox Product
                CB_listArea.DataSource        = msql.getlistArea();
                CB_listArea.DisplayMember     = "Tenkhuvuc";
                CB_listArea.ValueMember       = "Makhuvuc";
                CB_listArea.SelectedIndex     = 0;

               //load combox nhan vien giao
                CB_nhanviengiao.DataSource = msql.LayDanhSachNhanVien();
                CB_nhanviengiao.DisplayMember = "HoTenNhanVien";
                CB_nhanviengiao.ValueMember      = "Manhanvien";
                CB_nhanviengiao.SelectedIndex    = 0;

                //load combox nhan vien nhan hang
                CB_nhanviennhanhang.DataSource = msql.LayDanhSachNhanVien();
                CB_nhanviennhanhang.DisplayMember = "HoTenNhanVien";
                CB_nhanviennhanhang.ValueMember    = "Manhanvien";
                CB_nhanviennhanhang.SelectedIndex  = 0;

               //
                andoituong();                  
            #endregion
        }

        private void andoituong()
        {
            groupPanel_Kh.Visible = false;
            Xembaocao.Visible = false;
            Excel.Visible = false;
        }
        private void hiendoituong()
        {
            groupPanel_Kh.Visible = !false;
            Xembaocao.Visible = !false;
            Excel.Visible = !false;
            Excel.Enabled = false;
        }
               

        private void hienthisoluong()
        {
            SqlDataReader sqlrd;           
            string sqlstr_tensp = "select Tensp from sanpham";
            sqlrd = dtconn.ThucHienReader(sqlstr_tensp);
            while (sqlrd.Read())
            {                        
                dataGridView_SL.Rows.Add(dataGridView_SL.Rows.Count.ToString(),sqlrd[0].ToString(),0,"");
            }           
            dtconn.cnn.Close();
        }
        private void addcolumn()
        {
            DataGridViewTextBoxColumn  column1, column2, column3;
            column1 = new DataGridViewTextBoxColumn();
            column1.HeaderText = "*";
            column1.Width = 30;
            dataGridView_SL.Columns.Add(column1);
           
            cmb.HeaderText = "Tên sản phẩm";
            cmb.Name = "tsp";
            cmb.MaxDropDownItems = 10;
            cmb.DropDownWidth = 140;
            cmb.FlatStyle = FlatStyle.Flat;
            cmb.Width = 230;                 
            if(USE_CHECK_AlREADY_CB){
                dataGridView_SL.Columns.Add(cmb);
                USE_CHECK_AlREADY_CB = false;
            }
           

            column2 = new DataGridViewTextBoxColumn();
            column2.Name = "SL";
            column2.HeaderText = "Số lượng";
            column2.Width = 100;
            dataGridView_SL.Columns.Add(column2);

            column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Ghi chú";
            column3.Width = 100;
            dataGridView_SL.Columns.Add(column3);       
            dataGridView_SL.RowHeadersVisible = false;
        }
        private void loadcombo_sl()
        {
            //
            cmb.DataSource = msql.getlistProduct();
            cmb.DisplayMember = "Tensp";
            cmb.ValueMember = "Masp";
        }
       public delegate void delPassData(int value);// ham truyen du lieu wa cac form khac nhau
       public delegate void delPassDatatxt(string str);// ham truyen du lieu wa cac form khac nhau
       public delegate void delPassDatanguoinhan(string str);// ham truyen du lieu wa cac form khac nhau
       public delegate void delPassDatanguoixuat(string str);// ham truyen du lieu wa cac form khac nhau
       public delegate void delPassDatasp(DataTable _dttable);// ham truyen du lieu wa cac form khac nhau


       public  void Xembaocao_Click(object sender, EventArgs e)
        {
          Excel.Enabled = true;
          makv =  int.Parse(CB_listArea.SelectedValue.ToString());
          tenkv = CB_listArea.Text;
          nguoinhan = CB_nhanviennhanhang.Text;
          nguoixuat = CB_nhanviengiao.Text;

         //lay data tu datagirdview dua vao datatable
          copydatagirdviewtotable();
          //end 

          if (makv > 0)
          {
              //loctheoMKV();          
              fromkh =  new FormHienThiKhachHang();
              delPassData del = new delPassData(fromkh.getData);
              del(this.makv);
              delPassDatatxt deltxt = new delPassDatatxt(fromkh.getDatatxt);
              deltxt(tenkv);
              delPassDatanguoinhan delnguoinhan = new delPassDatanguoinhan(fromkh.getDatanguoinhan);
              delnguoinhan(nguoinhan);
              delPassDatanguoixuat delnguoixuat = new delPassDatanguoixuat(fromkh.getDatanguoixuat);
              delnguoixuat(nguoixuat);
              //truyen datatable           
              delPassDatasp delsp = new delPassDatasp(fromkh.getvaluefromdgv);             
              delsp(this.dttable);            
              fromkh.Show();          
          }
          else
          {
              MessageBox.Show("Mã khu vực không tồn tại!", "..: Cảnh báo :..", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          
        }

        private void BT_ChonKV_Click(object sender, EventArgs e)
        {
            hiendoituong();
            addcolumn();
            loadcombo_sl();
            groupPanel_Kh.Text = "Nhân viên bán hàng:" + " " + CB_nhanviennhanhang.Text + " " + "với số lượng";
       
        }

        private void BT_boqua_Click(object sender, EventArgs e)
        {
            andoituong();
        }
        private void loctheoMKV()
        {
            //ArrayList arrList_dskh = new ArrayList();
            //String strmasp="";
            SqlDataReader sqllaymasp;
           // string sqlstr_masp = "select sanpham from khachhang where makhuvuc = " + makv + " and tinhtrangkh=1 ";
            string sqlstr_masp = "select kh.Tencuahang,sp.masp as loai,kh.sanpham,sp.giasp,ck.Giachietkhau as GiaCK,(sp.giasp + ck.Giachietkhau) as Giaban ,CONVERT(VARCHAR(10),ck.ngaychietkhau, 103) as ngaychietkhau from khachhang kh inner  join chietkhau ck on kh.makh = ck.makh inner join sanpham sp on sp.masp =ck.masp where kh.makhuvuc=" + makv + " and tinhtrangkh=1";

            sqllaymasp = dtconn.ThucHienReader(sqlstr_masp);
            while (sqllaymasp.Read())
            {
                //strmasp = sqllaymasp[0].ToString();
                //String[] LoaiSanPham = strmasp.Split('+');
                //if (LoaiSanPham.Length > 1)
                //{
                //    for (int i = 0; i < LoaiSanPham.Length; i++)
                //    {
                //        laydskhachhangtheomasp(int.Parse(LoaiSanPham[i]));
                //    }
                //}
                //else
                //{
                //}      
                //dataGridView1.Rows.Add(dataGridView1.Rows.Count.ToString(), sqllaymasp[0].ToString(),sqllaymasp[1].ToString(),0,sqllaymasp[5].ToString(),0,0,0,0);  
            }
            dtconn.cnn.Close();
        }      
        private void copydatagirdviewtotable()
        {
            if (USE_CHECK_AlREADY_TABLE)
            {
                dttable.Columns.Add("tensp");
                dttable.Columns.Add("sl");               
                USE_CHECK_AlREADY_TABLE = false;
            }
            foreach (DataGridViewRow gridRow in dataGridView_SL.Rows)
            {
                if (gridRow.IsNewRow)
                    continue;
                DataRow dtRow = dttable.NewRow();
                dttable.Rows.Add(dtRow);
            }          
            for (int i = 0; i < dataGridView_SL.NewRowIndex; i++)
            {             
                dttable.Rows[i]["tensp"] = Convert.ToString((dataGridView_SL.Rows[i].Cells["tsp"] as DataGridViewComboBoxCell).FormattedValue.ToString());
                dttable.Rows[i]["sl"] = Convert.ToInt32(dataGridView_SL.Rows[i].Cells["SL"].Value);
            }            
        }

        private void Excel_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    ExportOptions CrExportOptions;

            //    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
            //    ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions();
            //    CrDiskFileDestinationOptions.DiskFileName = "c:\\test.xls";
            //    CrExportOptions = fromkh.report.ExportOptions;
            //    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            //    CrExportOptions.ExportFormatType = ExportFormatType.Excel;
            //    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
            //    CrExportOptions.FormatOptions = CrFormatTypeOptions;
            //    fromkh.report.Export();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}
            try
            {
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = "C:\\Giaokhachhang.pdf";
                CrExportOptions = fromkh.report.ExportOptions;                
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;                
                fromkh.report.Export();
                MessageBox.Show("Trích xuất thành công ra file Giaokhachhang.PDF", "..: Thông báo :..", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void FormDanhSachKhachHang_Load(object sender, EventArgs e)
        {

            this.reportViewer_DSKhachHang.RefreshReport();
        }
    }
}


